package com.stormrage.edu.serv.exam.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.exam.ExmExamDao;
import com.stormrage.edu.serv.edu.EduCourseServ;
import com.stormrage.edu.serv.edu.EduModuleServ;
import com.stormrage.edu.serv.exam.ExmAnswerServ;
import com.stormrage.edu.serv.exam.ExmExamServ;
import com.stormrage.entity.edu.EduCourse;
import com.stormrage.entity.edu.EduModule;
import com.stormrage.entity.exam.ExmExam;
import com.stormrage.entity.exam.rep.ExmExamCountRep;
import com.stormrage.entity.exam.rep.ExmExamRep;
import com.stormrage.entity.exam.req.ExmExamReq;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.BeanUtil;

/**
 * @Title: ExmExamServImpl.java
 * @Package com.stormrage.edu.serv.exam.impl
 * @Description: 考试试题 Service
 * @author www.stormragetech.com
 * @date 2016年4月24日
 * @version V1.0
 */
@Service
public class ExmExamServImpl implements ExmExamServ {

	private static final Logger logger = LoggerFactory.getLogger(ExmExamServImpl.class);

	@Autowired
	private ExmExamDao exmExamDao;

	@Autowired
	private EduCourseServ eduCourseServ;

	@Autowired
	private EduModuleServ eduModuleServ;

	@Autowired
	private ExmAnswerServ exmExamAnswerServ;

	@Override
	public Integer create(final ExmExam arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 create 】-【 STARTED 】");

		Integer index = exmExamDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 create 】-【 COMPLETED 】");
		return index;

	}

	@Override
	public void update(final ExmExam arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 update 】-【 STARTED 】");

		exmExamDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 update 】-【 COMPLETED 】");

	}

	@Override
	public void delete(Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 delete 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		exmExamDao.delete(map);

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 delete 】-【 COMPLETED 】");

	}

	@Override
	public Integer queryCountByRelateType(final Integer type, final Integer relateType, final Integer relateId)
			throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryCountByRelateType 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("relateType", relateType);
		map.put("relateId", relateId);

		Integer count = exmExamDao.queryCountByRelateType(map);

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryCountByRelateType 】-【 COMPLETED 】");
		return count;

	}

	@Override
	public ExmExam queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		ExmExam entity = exmExamDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<ExmExam> queryListByRelateId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryListByExamId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("relateId", arg0);

		List<ExmExam> array = exmExamDao.queryListByRelateId(map);

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryListByExamId 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public ExmExamCountRep queryRepByRelate(final ExmExamReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryRepByRelateType 】-【 STARTED 】");

		Integer scount = queryCountByRelateType(1, arg0.getRelateType(), arg0.getRelateId());
		Integer mcount = queryCountByRelateType(2, arg0.getRelateType(), arg0.getRelateId());
		Integer ccount = queryCountByRelateType(3, arg0.getRelateType(), arg0.getRelateId());

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryRepByRelateType 】-【 COMPLETED 】");
		return new ExmExamCountRep(scount, mcount, ccount);

	}

	@Override
	public ListRep queryListByRelateLimit(final ExmExamReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryListByExamId 】-【 STARTED 】");

		Map<String, Object> map = initQuery(arg0);

		Integer count = exmExamDao.queryCountByRelate(map);

		List<ExmExam> array = exmExamDao.queryListByRelateLimit(map);

		List<ExmExamRep> list = new ArrayList<ExmExamRep>();
		int seq = arg0.getPage().getStartPage() + 1;

		Iterator<ExmExam> it = array.iterator();
		while (it.hasNext()) {

			ExmExam exam = it.next();
			ExmExamRep rep = null;

			Integer answerCount = exmExamAnswerServ.queryCountByExamId(exam.getId());

			if (exam.getRelateType().equals(0)) {
				EduCourse course = eduCourseServ.queryById(exam.getRelateId());
				rep = ExmExamRep.instance(exam, seq, answerCount, course.getCourse());
			}
			if (exam.getRelateType().equals(1)) {
				EduModule module = eduModuleServ.queryById(exam.getRelateId());
				rep = ExmExamRep.instance(exam, seq, answerCount, module.getModule());
			}
			list.add(rep);
			seq++;

		}

		ListRep rep = ListRep.instance(count, list);

		logger.debug(" 【 Serv 】- 【 ExmExamServ 】- 【 queryListByExamId 】-【 COMPLETED 】");
		return rep;

	}

	private Map<String, Object> initQuery(final ExmExamReq arg0) {

		Map<String, Object> map = BeanUtil.initQuery(arg0.getPage());
		map.put("relateType", arg0.getRelateType());
		map.put("relateId", arg0.getRelateId());

		StringBuffer query = new StringBuffer("");
		if (null != arg0.getQuestion() && arg0.getQuestion().length() > 0) {
			query.append("question LIKE :question AND ");
			map.put("question", "%" + arg0.getQuestion() + "%");
		}

		String sql = query.toString();
		if (null != sql && sql.length() > 0)
			map.put("query", " AND " + sql.substring(0, sql.length() - 4));
		else
			map.put("query", "");

		return map;

	}

}
